//
//  SFiltrateView.m
//  EasyRetail
//
//  Created by Samuel on 15/5/22.
//
//

#import "SFiltrateView.h"

#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
@implementation SFiltrateView
{
    CGRect frames;
    NSMutableArray *dataArray;
    NSArray *titleArray;
    NSMutableArray *tableViewArray;
    
    UIButton *saveLastBtn;
    
}

- (id)init
{
    return [self initWithFrame:CGRectZero];
}

- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        [self commonInitWithFrame:frame];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self commonInitWithFrame:CGRectZero];
    }
    
    return self;
}
- (void)commonInitWithFrame:(CGRect)frame
{
    frames = frame;
    dataArray = [[NSMutableArray alloc]init];
    titleArray = [[NSArray alloc]init];
    tableViewArray = [[NSMutableArray alloc]init];
}


- (void)setDataSource:(NSObject<SFiltrateViewDelegate> *)dataSource
{
    _dataSource = dataSource;
    float widths,heights;
    if (frames.size.width&&frames.size.height) {
        widths = frames.size.width;
        heights = frames.size.height;
    }
    else
    {
        widths = [ UIScreen mainScreen ].bounds.size.width;
        heights = [self.dataSource SFiltrateView:self heightForRowAtIndexPath:0]*([dataArray count]);
    }
    self.ftableViews = [[UITableView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-heights, widths, heights)];
    self.ftableViews.delegate = self;
    self.ftableViews.dataSource = self;
    self.ftableViews.bounces = NO;
    self.ftableViews.showsVerticalScrollIndicator = NO;
    [self.ftableViews setAlpha:0];
    [self.getTargets.view addSubview:self.ftableViews];
    [self.getTargets.view addSubview:self];
    /**
     *  获取填充所有数据
     */
    titleArray = [self.dataSource sectionOfSFiltrateView:self];
    for (int i = 0; i <[titleArray count]; i++) {
        NSMutableArray *getdatas = [NSMutableArray array];
        for (int j = 0; j<[self.dataSource numberOfRowsInSectionOfSFiltrateView:self andSection:i]; j++) {
            [getdatas addObject:[self.dataSource SFiltrateView:self viewForRowAtIndex:i andSection:i]];
        }
        if ([getdatas count]>0) {
            [dataArray addObject:getdatas];
        }
    }
    
    /**
     *  穿件title按钮
     */
    for (int k = 0 ;k < [titleArray count];k++) {
        NSString *gettitles = [titleArray objectAtIndex:k];
        UIButton *buttons = [UIButton buttonWithType:UIButtonTypeCustom];
        [buttons setTitle:gettitles forState:UIControlStateNormal];
        [buttons setBackgroundColor:[UIColor whiteColor]];
        [buttons setFrame:CGRectMake(self.frame.size.width/[titleArray count]*k, 0, self.frame.size.width/[titleArray count], self.frame.size.height)];
        [buttons addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
        [buttons setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [buttons setTitleColor:RGBA(127, 155, 66, 1) forState:UIControlStateSelected];
        buttons.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:buttons];
    }
    
}
- (void)closeCell;
{
    [self buttonAction:saveLastBtn];
}
- (void)buttonAction:(UIButton*)sender
{
    if (![sender.titleLabel.text isEqualToString:saveLastBtn.titleLabel.text]) {
        [UIView animateWithDuration:.3 animations:^{
            [self.ftableViews setCenter:CGPointMake(self.ftableViews.center.x, self.ftableViews.center.y-self.ftableViews.frame.size.height)];
        } completion:^(BOOL finished) {
            [self.ftableViews setAlpha:0];
            saveLastBtn.selected = NO;
            saveLastBtn = nil;
            [self buttonSettings:sender];
        }];
    }
    else
    {
        [self buttonSettings:sender];
    }
    
}
- (void)buttonSettings:(UIButton*)sender
{
    if (sender.selected == NO) {
        /**
         *  填充数据
         */
        int returnCount =0;
        float cellAllHigh = 0;
        for (int i = 0 ; i <[titleArray count]; i++) {
            if ([sender.titleLabel.text isEqualToString:[titleArray objectAtIndex:i]]) {
                returnCount = i;
                tableViewArray = [dataArray objectAtIndex:i];
                for (int s = 0; s<[tableViewArray count]; s++) {
                    cellAllHigh = [self.dataSource SFiltrateView:self heightForRowAtIndexPath:s]+cellAllHigh;
                }
            }
        }
        [self.ftableViews reloadData];
        /**
         *  设置tableView 和self
         */
        [self.ftableViews setFrame:CGRectMake(0, self.frame.size.height-cellAllHigh, self.frame.size.width, cellAllHigh)];
        /**
         *  动画展现
         */
        [self.ftableViews setAlpha:1];
        [UIView animateWithDuration:.3 animations:^{
            [self.ftableViews setCenter:CGPointMake(self.ftableViews.center.x, self.ftableViews.center.y+self.ftableViews.frame.size.height)];
        } completion:^(BOOL finished) {
            [self.dataSource SFiltrateView:self didSelectSection:returnCount];
        }];
        /**
         *  控制选择
         */
        sender.selected = YES;
        saveLastBtn = sender;
    }
    else
    {
        [UIView animateWithDuration:.3 animations:^{
            [self.ftableViews setCenter:CGPointMake(self.ftableViews.center.x, self.ftableViews.center.y-self.ftableViews.frame.size.height)];
        } completion:^(BOOL finished) {
            [self.ftableViews setAlpha:0];
        }];
        
        sender.selected = NO;
        saveLastBtn = nil;
    }
}
#pragma mark ******************** tableView Delegate ********************

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [tableViewArray count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return [self.dataSource SFiltrateView:self heightForRowAtIndexPath:indexPath.row];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
//    static NSString *TableSampleIdentifier = @"cfcell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cfcell%ld",(long)indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UIView *getViews = [tableViewArray objectAtIndex:indexPath.row];
    getViews.userInteractionEnabled = NO;
    [cell.contentView addSubview:getViews];
//    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
//    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    [self.dataSource SFiltrateView:self didSelectRowAtIndexPath:indexPath.row];
}


@end
